layui.use(['jquery','layer','table'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;

    var couponName = "";
    var online = "";
    var grant = "";
    var startDate = "";
    var endDate = "";
    select("/Coupons/selectCoupons?couponName="+couponName+"&online="+online+"&grant="+grant);
    function select(url) {
        table.render({
            elem:"#root",
            url:url,
            toolbar:"#lefttool",
            defaultToolbar: ['','',''],
            page:true,
            limit:5,
            limits:[5,10,15,20],
            size: 'lg',
            cols:[[
                {type:"checkbox"},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"startDate",title:"开始时间",align:"center"},
                {field:"endDate",title:"结束时间",align:"center"},
                {field:"online",title:"上线/不上线",align:"center",templet:function (data) {
                    if(data.online=="2"){
                        var radio = '<a href="javascript:updateStatus('+data.couponId+","+data.online+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上线|不上线" lay-filter="switchTest" value="'+data.online+'" checked></a>'
                    }else{
                        var radio = '<a href="javascript:updateStatus('+data.couponId+','+data.online+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上线|不上线" lay-filter="switchTest" value="'+data.online+'"></a>'
                    }
                    return radio;
                }},
                {field:"grant",title:"发放方式",align:"center",templet:function (data) {
                        if(data.grant=="2"){
                            return "自动发放";
                        }else if(data.grant=="3"){
                            return "活动领取";
                        }else if(data.result=="4"){
                            return "人工发放";
                        }else{
                            return "无限制";
                        }
                    }},
                {field:"issueQuantity",title:"发行数",align:"center"},
                {field:"used",title:"已使用数量",align:"center"},
                {title:"操作",align:"center",templet:function (data) {
                    return "<div>"+
                                "<a href='javascript:chakan("+data.couponId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "<a href='javascript:shanchu("+data.couponId+");' style='color: #0EAA9E'>删除</a><br/>"+
                            "</div>"
                }}
            ]]
        })
    };
    //刷新
    $("#shuaxin").click(function () {
        location.reload();
    });
    //修改状态
    updateStatus = function updateStatus(couponId,online) {

        if(online==2){
            var online2 = 3;
        }else{
            var online2 = 2;
        };

        var updateId = localStorage.getItem("userId");
        var updateDate = new Date();
        updateDate = getTime(updateDate);

        $.ajax({
            type:"post",
            dataType:"json",
            data:{
                "couponId":couponId,
                "online":online2,
                "updateId":updateId,
                "updateDate":updateDate
            },
            url:"/Coupons/updateCoupon",
            success:function (data) {
                if(data){
                    layer.msg("修改成功");
                }else{
                    layer.msg("修改失败");
                }
                location.reload();
            }
        })
    };
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

    //查看
    chakan = function chakan(couponId){
        $.ajax({
            type:"post",
            dataType:"json",
            title:"查看优惠券详情",
            data:{"couponId":couponId},
            url:"/Coupons/selectCouponById",
            success:function (data) {
                selectTable(data);
            }
        })
    };
    function selectTable(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看优惠券详情",
            content:"/coupon/coupon-see.html",
            area:['600px', '500px'],
            btn:["关闭"],
            yes:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#couponCode").val(data.data.couponCode);
                obj.find("#couponName").val(data.data.couponName);
                obj.find("#couponPrice").val(data.data.couponPrice);
                obj.find("#issueQuantity").val(data.data.issueQuantity);
                obj.find("#used").val(data.data.used);
                obj.find("#startDate").val(data.data.startDate);
                obj.find("#endDate").val(data.data.endDate);
                //状态
                var online = data.data.online;
                if(online=="2"){
                    online="上线";
                }else{
                    online="不上线";
                }
                obj.find("#online").val(online);
                //发放方式
                var grant = data.data.grant;
                if(grant=="2"){
                    grant="上线";
                }else if(grant=="3"){
                    grant="活动领取";
                }else if(grant=="4"){
                    grant="人工发放";
                }else{
                    grant="无限制";
                }
                obj.find("#grant").val(grant);
                //类型
                var type = data.data.type;
                if(type=="2"){
                    type="商品";
                }else{
                    type="视频";
                }
                obj.find("#type").val(type);

            }
        });
    };
    //删除
    shanchu = function shanchu(couponId) {
        layer.confirm('是否确定删除此条数据', {
                icon: 3, title: '提示'
            },
            function (index) {
                //通过id删除表格数据
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"couponId":couponId},
                    url:"/Coupons/deleteCoupon",
                    success:function (data) {
                        if(data){
                            layer.msg("删除成功");
                            location.reload();
                        }else{
                            layer.msg("删除失败");
                        }
                    }
                });
            }
        )
    };

    $("#jiansuo").click(function () {
            var obj;
            layer.open({
                type:2,
                title:"高级检索",
                content:"/coupon/coupon-search.html",
                area: ['600px', '500px'],
                shade: 0,
                btn:["确定","取消"],
                yes:function (index) {
                   var couponName = obj.find("#couponName").val();
                   var grant = obj.find("#grant").val();
                   var online = obj.find("#online").val();
                   var startDate = obj.find("#startDate").val();
                   var endDate = obj.find("#endDate").val();
                   select("/Coupons/selectCoupons?couponName="+couponName+"&online="+online+"&grant="+grant+"&startDate="+startDate+"&endDate="+endDate);
                   layer.close(index);
                },btn2:function (index) {
                    layer.close(index);
                },success:function (layero,index) {
                   obj = layero.find('iframe').contents();
                }
            })
    });

    $("#sousuo").click(function () {
       var couponName = $("#couponName").val();
       var grant = $("#grant").val();
       var online = $("#online").val();
       select("/Coupons/selectCoupons?couponName="+couponName+"&online="+online+"&grant="+grant);
    });

    $("#chongzhi").click(function () {
        $("#couponName").val("");
        $("#grant").val("");
        $("#online").val("");
        select("/Coupons/selectCoupons?couponName="+couponName+"&online="+online+"&grant="+grant);
    });
    //添加
    addCoupon = function addCoupon(){
        layer.open({
            type:2,
            title:"添加优惠券",
            content:"/coupon/coupon-add.html",
            area: ['800px', '600px'],
            shade: 0,
            success:function (layero,index) {
                var obj = layero.find("iframe").contents();
            }
        })
    };

    $("#shuaxin").click(function () {
        location.reload();
    });
    //跳转
    goBack=function goBack(){
        //通过页面查找iframe
        parent.$('#recordLog').val($('#examine').text());
        parent.before();
        return false;
    };
});
